package de.tempel.pong.collison;

import java.awt.geom.Point2D;
import java.util.List;

import processing.core.PApplet;
import de.tempel.pong.objects.Ball;
import de.tempel.pong.objects.Racket;
import de.tempel.pong.objects.basic.Vector;

/**
 * 
 * Manager für DiscreteCollisionDetection.
 * 
 * @author mathias
 * 
 */
public class DiscreteCollisionManager extends AbstractCollisionManager {

	/**
	 * 
	 * @param ball
	 * @param rackets
	 * @param window
	 */
	public DiscreteCollisionManager(Ball ball, List<Racket> rackets,
			PApplet window) {
		super(ball, rackets, window);
	}

	@Override
	public void checkCollisions(float tpf) {

		Vector oldBallDirection = ball.getMovement();

		/**
		 * Obere Kante
		 */
		Point2D ballBasePoint = ball.getPosition();
		if (ballBasePoint.getY() < 0) {
			Vector newBallDirection = new Vector(oldBallDirection.getX(),
					oldBallDirection.getY() * -1);
			ball.setMovement(newBallDirection);
			return;
		}

		/**
		 * Untere Kante
		 */
		if (ballBasePoint.getY() + ball.getWidth() > window.getHeight()) {
			Vector newBallDirection = new Vector(oldBallDirection.getX(),
					oldBallDirection.getY() * -1);
			ball.setMovement(newBallDirection);
			return;
		}

		/**
		 * Kollisionen mit Schläger
		 */
		if (rackets != null) {

			for (Racket r : rackets) {

				if (r.getRectangle().intersects(ball.getRectangle())) {

					// System.out.println("Has collision!");
					Vector newBallDirection = new Vector(
							oldBallDirection.getX() * -1,
							oldBallDirection.getY());
					ball.setMovement(newBallDirection);

					return;

					// Position muss leicht angepasst werden. Wenn sich der Ball
					// im Schläger befindet kann es direkt beim nächsten Aufruf
					// wieder eine Kollision geben.
					// float newXPosition;
					// if (r.getCorner() == Corner.LEFT) {
					// newXPosition = r.getPosition().getX() + r.getWidth()
					// + 1;
					// } else {
					// newXPosition = r.getPosition().getX() - ball.getWidth()
					// + -1;
					// }
					// ball.setPosition(new Point(newXPosition,
					// ball.getPosition()
					// .getY()));
					//
					// window.fill(130);
					// ball.draw(window);
					// window.fill(255);

				}

			}

		}
	}
}
